<?php
namespace app\common\model;
use think\Model;

/**
 * 收藏相关
 */
class Collect extends Model {
	/**
	 * [getList 收藏列表]
	 * @param  array   $where [条件]
	 * @param  integer $p     [分页]
	 * @param  integer $type     [收藏类型 1：需求  2：日记 3：服务]
	 * @return [type]         [description]
	 */
	public function getList($where = [],$type = 1,$p = 1){
		$model = $this->alias('c');
		if($type == 1){
			$model->join(['bz_need' => 'f'],'f.id = c.foreign_key and c.type = 1','left');
		}
		if($type == 2){
			$model->join(['bz_nearby' => 'f'],'f.id = c.foreign_key and c.type = 2','left');
		}
		if($type == 3){
			$model->join(['bz_service' => 'f'],'f.id = c.foreign_key and c.type = 3','left');
		}
		$list = $model->join(['bz_user' => 'u'],'u.id = f.user_id','left')
		->field('c.foreign_key as id,f.title,f.image,u.nick_name,u.photo_path,kaopu,service_count,u.tag,FROM_UNIXTIME(c.add_time,"%Y-%m-%d %H:%i") as add_time')
		->where($where)
		->order('c.add_time desc')
		->limit(($p-1)*config('page_size'),config('page_size'))
		->select();
		
		foreach($list as $key => $val){
			$list[$key]['image'] = empty($val['image']) ? [] :explode(',',$val['image']);
			$list[$key]['tag'] = $val['tag'] == '' ? [] :explode(',',$val['tag']);
		}
		return $list;
	}
}
